define(["jquery", "bootstrap", "backend", "table", "form"], function ($, undefined, Backend, Table, Form) {
  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: "dicts/jobtitle/index" + location.search,
          add_url: "dicts/jobtitle/add",
          edit_url: "dicts/jobtitle/edit",
          del_url: "dicts/jobtitle/del",
          multi_url: "dicts/jobtitle/multi",
          import_url: "dicts/jobtitle/import",
          table: "dicts_jobtitle",
        },
      });

      var table = $("#table");

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: "id",
        sortName: "id",
        columns: [
          [
            { checkbox: true },
            { field: "id", title: __("Id") },
            { field: "jobtitle", title: __("Jobtitle"), operate: "LIKE" },
            {
              field: "createtime",
              title: __("Createtime"),
              operate: "RANGE",
              addclass: "datetimerange",
              autocomplete: false,
              formatter: Table.api.formatter.datetime,
            },
            {
              field: "updatetime",
              title: __("Updatetime"),
              operate: "RANGE",
              addclass: "datetimerange",
              autocomplete: false,
              formatter: Table.api.formatter.datetime,
            },
            {
              field: "operate",
              title: __("Operate"),
              table: table,
              events: Table.api.events.operate,
              formatter: Table.api.formatter.operate,
              buttons: [
                {
                  name: "Delete",
                  title: __("删除"),
                  classname: "btn btn-xs btn-danger btn-ajax btn-deleteit",
                  icon: "fa fa-trash",
                  url: "dicts/jobtitle/del",
                  confirm: "确认要删除吗？",
                  refresh: true, // 刷新表格
                  success: function (data, ret) {
                    Layer.alert("删除成功");
                    // 刷新表格
                    $(document).ready(function () {
                      // 模拟点击按钮
                      $("#jobtitle-refresh").trigger("click");
                    });
                  },
                  error: function (data, ret) {
                    console.log("ret = ", ret);
                    ret.msg.includes("SQLSTATE[23000]")
                      ? Layer.alert("不能够删除，因为有其他表正在使用该行数据，在其他表中删除所有该行数据后，方可正常删除")
                      : Layer.alert("其他原因导致删除失败");
                  },
                },
              ],
            },
          ],
        ],
      });

      // 为表格绑定事件
      Table.api.bindevent(table);
    },
    add: function () {
      Controller.api.bindevent();
    },
    edit: function () {
      Controller.api.bindevent();
    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      },
    },
  };
  return Controller;
});
